package com.wuwei.aliocr.utils;

import com.alibaba.fastjson.JSON;
import com.wuwei.aliocr.domain.EnglishListening;
import com.wuwei.aliocr.domain.EnglishListeningForAdd;
import com.wuwei.aliocr.domain.ListenQues;
import com.wuwei.aliocr.result.Result;
import com.wuwei.aliocr.service.TikuService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetTitleFromTxt {




    @Autowired
    private TikuService tikuService;

    /**
     * 将txt文件里的英语题目转换并存到数据库中
     */
    @SneakyThrows
    private void getTitle() {
        BufferedReader reader = null;
        EnglishListeningForAdd englishListening = new EnglishListeningForAdd();
        ArrayList<ListenQues> quesList = new ArrayList<>();
        ListenQues temp = new ListenQues();
        try {
            // 创建一个文件读取器对象，指定要读取的文件路径和文件名
            reader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\listen.txt"));
            // 读取文件内容并输出
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                if (!tempString.equals("")) {
                    while (tempString.length()-tempString.indexOf("套)")==2){
                        String title = tempString.substring(0,tempString.indexOf("真题"))+tempString.substring(tempString.indexOf('第'),tempString.length()-1);
                        System.out.println(title);
                        englishListening.setName(title);
                        break;
                    }
                    while (tempString.indexOf("http")==0){
                        System.out.println(tempString);
                        englishListening.setSrc(tempString);
                        break;
                    }
                    while ((tempString.indexOf('A')-tempString.indexOf(')'))==-1){
                        System.out.println(tempString);
                        temp.setItem1(tempString);
                        break;
                    }
                    while (tempString.charAt(0)=='B'){
                        System.out.println(tempString);
                        temp.setItem2(tempString);
                        break;
                    }
                    while (tempString.charAt(0)=='C'){
                        System.out.println(tempString);
                        temp.setItem3(tempString);
                        break;
                    }
                    while ((tempString.charAt(0)=='D')&&(tempString.charAt(1)==')')){
                        System.out.println(tempString);
                        temp.setItem4(tempString);
                        ListenQues listenQues = new ListenQues();
                        BeanUtils.copyProperties(temp,listenQues);
                        quesList.add(listenQues);
                        break;
                    }
                    while ((isDigit(tempString.charAt(0)))&&(isNotKuoHao(tempString))){
                        int index = Integer.parseInt(tempString.substring(0,tempString.indexOf('.')));
                        System.out.println(index);
                        ListenQues listenQues = quesList.get(index-1);
                        listenQues.setAnswer(tempString);
                        quesList.set(index-1,listenQues);
                        break;
                    }
                }
            }
            englishListening.setQuestion(quesList);
            System.out.println(englishListening);//englishListening上传到数据库
            Result result = tikuService.saveListenFromTxt(englishListening);
            reader.close(); // 关闭文件读取器
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    // ignore
                }
            }
        }
    }




    private boolean isNotKuoHao(String tempString) {
        if (tempString.indexOf(')')==-1){//表明无括号
            return true;
        }
        return false;
    }

    private boolean isDigit(char ch) {
        int ascii = (int) ch; // 获取字符的ASCII码值
        if (ascii >= 49 && ascii <= 57) { // 判断是否在1-9的范围内
            return true;
        }
        return false;
    }
}
